package com.ahccc.codegenerator.vo;

import com.baomidou.mybatisplus.annotation.DbType;
import lombok.Data;
import lombok.experimental.Accessors;

@Data
@Accessors(chain = true)
public class DbConnInfo {

    private String type;

    private String host;

    private String port;

    private String schema;

    private String username;

    private String password;

    public static final String MYSQL_DRIVER = "com.mysql.cj.jdbc.Driver";

    public static final String ORACLE_DRIVER = "oracle.jdbc.driver.OracleDriver";

    public static final String SQL_SERVER_DRIVER = "com.microsoft.jdbc.sqlserver.SQLServerDriver";

    public static String getDriveName(String dbType){
        switch (DbType.getDbType(dbType)) {
            case MYSQL:
                return MYSQL_DRIVER;
            case ORACLE:
                return ORACLE_DRIVER;
            case SQL_SERVER:
                return SQL_SERVER_DRIVER;
            default:
                return MYSQL_DRIVER;
        }
    }

    public static String getUrl(String dbType, String host, String port, String schema){
        switch (DbType.getDbType(dbType)) {
            case MYSQL:
                return getMySqlUrl(host, port, schema);
            case ORACLE:
                return getOracleUrl(host, port, schema);
            case SQL_SERVER:
                return getSqlServerUrl(host, port, schema);
            default:
                return getMySqlUrl(host, port, schema);
        }
    }

    public static String getMySqlUrl(String host, String port, String schema){
        return "jdbc:mysql://" + host + ":" + port + "/" + schema +
                "?useUnicode=true" +
                "&characterEncoding=utf8" +
                "&useSSL=true" +
                "&serverTimezone=UTC" +
                "&zeroDateTimeBehavior=CONVERT_TO_NULL";
    }

    public static String getOracleUrl(String host, String port, String schema){
        return "jdbc:oracle:thin:@" + host + ":" + port + ":" + schema;
    }


    public static String getSqlServerUrl(String host, String port, String schema){
        return "jdbc:sqlserver://" + host + ":" + port + ";DatabaseName=" + schema;
    }
}
